package com.gxa.controller.employee;

import com.gxa.common.R.ResponseData;
import com.gxa.service.employee.ExcelService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;

//import static com.gxa.controller.employee.PhotoUploadController.getNowTime;

/**
 * Excel文件上传导出controller层
 */
@Controller
@Api(value = "Excel文件批量导入与下载",tags = "Excel文件批量导入、下载API")
@RequestMapping("/excel")
public class ExcelController {
    @Autowired(required = false)
    private ExcelService excelService;

    //上传Excel文件
    @RequestMapping(value = "/ExcelUpload",method = RequestMethod.POST) //get不具备上传文件功能
    @ApiOperation(value = "批量导入Excel文件",notes = "将员工信息Excel文件上传", response = ResponseData.class)
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = false, dataType = "String")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData ExcelUpload(MultipartFile file,String deptId, HttpServletRequest request) throws IOException, ParseException {
        ResponseData responseData = excelService.excelRead(file, deptId);
        return responseData;
    }

    //导出Excel文件
    @RequestMapping(value = "/ExcelExport",method = RequestMethod.POST) //get不具备上传文件功能
    @ApiOperation(value = "导出Excel文件",notes = "将数据库员工信息写入Excel文件导出", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData ExcelExport(@RequestParam(required = false) String deptId, HttpServletResponse response) throws IOException {
        ResponseData responseData = excelService.excelWrite(response,deptId);
        return responseData;
    }
}
